<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close
             @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" icon="el-icon-plus" v-auth="'client.add'" @click="add">新增</el-button>
                </div>
                <div class="right-panel">
                        <div class="right-panel-search">
                        <el-input v-model="search.customer_name" placeholder="客户名(精确)" clearable @input="upsearch"></el-input>
                        <el-input v-model="search.keyword" ref="keywordInput" placeholder="关键字查询" clearable @input="upsearch"></el-input>
                        <!-- <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button> -->
                    </div>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" highlight-current-row @row-click="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                    <el-table-column width="1"></el-table-column>
                    <el-table-column type="index" label="" align="center" width="50"></el-table-column>
                    <el-table-column label="客户名称" prop="customer_name" align="center"></el-table-column>
                    <el-table-column label="别名" prop="help_remember" align="center"></el-table-column>
                    <el-table-column label="联系方式" prop="mobile" align="center"></el-table-column>
                    <el-table-column label="账号结余" prop="balance" align="center"></el-table-column>
                    <el-table-column label="地址" prop="address" align="center"></el-table-column>
                    <!-- <el-table-column label="地区" prop="" align="center"></el-table-column> -->
                    <el-table-column label="创建人" prop="salesman_name" align="center"></el-table-column>
                </scTable>
            </el-main>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
    components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择客户',
				edit: '编辑客户',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			apiObj: this.$API.basic.client.list,
			selection: [],
			search: {
				keyword: null,
			},
            dialog: {
				save: false,
			},
            type: "",
            query:{
                system_disable :0
            },
		}
	},
	mounted() {
        setTimeout(()=>{ 
            this.$nextTick(()=>{
                this.$refs.keywordInput.focus()
            })
        },100)
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.type = data
		},
        selectAll() {
            this.$message.error("只能单选！")
            this.$refs.table.clearSelection()
		},
        // 选择事件
        selectionChange(selection) {
            this.$emit('success', selection)
            this.visible = false;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.type)
            this.visible = false;
        },
        //商品新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新商品新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
	}
}
</script>

<style></style>
